package cn.edu.bjut.homework.chapter2;

public class ChapterTwo {
    public static void main(String[] args) {
        System.out.println(question1());
        System.out.println(question2());
        question3();
        System.out.println(question4("G", 6, "F", 17, 30));
        System.out.println(question4("H", 1, "M", 10, 587));
    }

    public static int question1() {
        int sum = 0;
        for (int i = 1; i <= 20; i++) {
            int val = (int) Math.pow(2, i);
            // System.out.println(val);
            sum += val - 1;
        }
        return sum;
    }

    public static double question2() {
        double limit = Math.pow(10, -5);
        double sum = 0;
        double i = 1;
        while (1 / i >= limit) {
            if (i % 2 == 0) {
                sum -= 1 / i;
            } else {
                sum += 1 / i;
            }
            i++;
        }
        return sum;
    }

    public static void question3() {
        int a, b, c;
        int num;
        int js = 0, sum = 0;
        for (num = 100; num < 1000; num++) {
            a = num / 100;
            b = num / 10 % 10;
            c = num % 10;
            if (a != 9 && b != 9 && c != 9) {
                sum += num;
                js++;
            }
        }
        System.out.println("没有9的三位数的个数: " + js);
        System.out.println("没有9的三位数之和: " + sum);
    }

    public static String question4(String section, int class_num, String subclass, int main_group, int sub_group) {
        String result = "";
        result = String.format("%s%02d%s%4d/%-5d", section, class_num, subclass, main_group, sub_group);
        return result;
    }
}
